#include <iostream>
#include <vector>
#include <queue>

using namespace std;
typedef pair<int, int> PII;

const int N = 1e4 + 10;
const int INF = 2147483647;
int n, m, s;
int dist[N];
bool st[N];
vector<PII> edges[N];

void spfa(int s)
{
    for(int i = 0; i <= n; i++) dist[i] = INF;
    queue<int> q;
    dist[s] = 0;
    st[s] = true;
    q.push(s);
    while(q.size())
    {
        auto u = q.front(); q.pop();
        st[u] = false;
        for(auto& t : edges[u])
        {
            int v = t.first, w = t.second;

            if(dist[u] + w < dist[v])
            {
                dist[v] = dist[u] + w;
                if(!st[v])
                {
                    st[v] = true;
                    q.push(v);
                }
            }
        }
    }
    for(int i = 1; i <= n; i++) cout << dist[i] << " ";
}

int main()
{
    cin >> n >> m >> s;
    for(int i = 1; i <= m; i++)
    {
        int u, v, w; cin >> u >> v >> w;
        edges[u].push_back({v, w});
    }
    spfa(s);
    return 0;
}

